﻿// Extensions Made Easy - Copyright 2012 by Jan Van der Haegen - All rights reserved, use for educational purposes only.

using System;
using System.Collections.Generic;
using System.Linq;
using ExtensionsMadeEasy.ClientAPI.Commands;
using Microsoft.LightSwitch.Runtime.Shell.ViewModels.Commands;

namespace ExtensionsMadeEasy.Presentation.Commands
{
    public sealed class EasyCommandGroup : IShellCommandGroup
    {
        private class CompareStrings : IComparer<string>
        {
            // Because the class implements IComparer, it must define a 
            // Compare method. The method returns a signed integer that indicates 
            // whether s1 > s2 (return is greater than 0), s1 < s2 (return is negative),
            // or s1 equals s2 (return value is 0). This Compare method compares strings. 
            public int Compare(string s1, string s2)
            {
                return string.Compare(s1, s2, StringComparison.OrdinalIgnoreCase);
            }
        }

        private static int GroupNumber = 0;

        private IList<IShellCommand> commands = new List<IShellCommand>();
        private readonly string groupName = String.Format("EasyCommandGroup{0}", ++GroupNumber);

        public IEnumerable<IShellCommand> Commands
        {
            get
            {
                return this.commands;
            }
        }

        public void AddCommand(IShellCommand command)
        {
            this.commands.Add(command);
            this.commands = new List<IShellCommand>(
                this.commands.OrderBy((c) => (c is EasyCommand ? ((EasyCommand)c).SortName : c.DisplayName), new CompareStrings()));
        }
         
        public string DisplayName { get; set; }
         
        public string Name
        {
            get
            {
                return this.groupName;
            }
        }
    }
}